package com.yc.cloud.portal.config;

import com.yc.cloud.basic.config.PathUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.util.Properties;

@Configuration
public class FileAccessConfig implements WebMvcConfigurer {

    /**
     * 访问外部文件配置，访问D盘下文件
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置server虚拟路径，handler为jsp中访问的目录，locations为files相对应的本地路径
        Properties props = System.getProperties();
        String systemName = props.getProperty("os.name");
        System.out.println("系统:" + systemName);
        if (systemName.contains("Windows")) {
            registry.addResourceHandler("/resource/**").addResourceLocations("file:" + PathUtils.getBasePath() + File.separator);
        } else {
            registry.addResourceHandler("/resource/**").addResourceLocations("file:///" + PathUtils.getBasePath() + File.separator);
        }
    }
}
